const { DataTypes } = require('sequelize');
const sequelize = require('@shared/utils/database');

// 订单状态枚举
const ORDER_STATUS = {
  PENDING: 'pending',       // 待付款
  DELIVERY: 'delivery',     // 待发货
  SHIPPING: 'shipping',     // 待收货
  COMPLETED: 'completed',   // 已完成
  CANCELED: 'canceled'      // 已取消
};

const Order = sequelize.define('Order', {
  id: {
    type: DataTypes.BIGINT,
    primaryKey: true,
    autoIncrement: true,
    comment: '订单ID'
  },
  order_no: {
    type: DataTypes.STRING(32),
    allowNull: false,
    comment: '订单编号'
  },
  user_id: {
    type: DataTypes.BIGINT,
    allowNull: false,
    comment: '用户ID'
  },
  total_amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '订单总金额'
  },
  pay_amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '实付金额'
  },
  freight_amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    defaultValue: 0.00,
    comment: '运费金额'
  },
  address_snapshot: {
    type: DataTypes.JSON,
    allowNull: false,
    comment: '收货地址快照'
  },
  status: {
    type: DataTypes.STRING(20),
    allowNull: false,
    defaultValue: ORDER_STATUS.PENDING,
    comment: '订单状态：pending-待付款 delivery-待发货 shipping-待收货 completed-已完成 canceled-已取消'
  },
  pay_time: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '支付时间'
  },
  delivery_time: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '发货时间'
  },
  receive_time: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '收货时间'
  }
}, {
  tableName: 'order',
  timestamps: true,
  createdAt: 'create_time',
  updatedAt: 'update_time',
  indexes: []
});

// 添加状态枚举
Order.STATUS = ORDER_STATUS;

module.exports = Order; 